package rp.group;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import utils.COLOR;
import utils.UTIL;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CMDGroup implements CommandExecutor {
    private Map<String, Boolean> conf;

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
        boolean isAdmin = sender.hasPermission("minecraft");
        boolean isPlayer = sender instanceof Player;
        Player player = isPlayer ? (Player) sender : null;
        Player target = null;

        if (args.length == 0)
            args = new String[]{"menu"};
        if (getCmd(args[0]) == "menu") {
            if (isPlayer) {
                player.openInventory(UTIL.MENU("主菜单", 6,
                        new HashMap<Integer, HashMap>() {{
                            put(8, new HashMap() {{
                                put("NAME", COLOR.RED.getValue() + "×");
                                put("MATERIAL", Material.BARRIER);
                                put("LORE", new ArrayList<String>() {{
                                    add(COLOR.CYAN.getValue() + "左击" + COLOR.GREEN.getValue() + "返回主菜单");
                                    add(COLOR.CYAN.getValue() + "右击" + COLOR.GREEN.getValue() + "查看帮助");
                                }});
                                put("RIGHT", "/r menu");
                                put("RIGHT", "/group help");
                            }});
                        }}
                ));
                return true;
            }
            args = new String[]{"help"};
        }
        if (getCmd(args[0]) == "help") {
            UTIL.SEND(sender, new String[]{
                    COLOR.GOLD.getValue() + "/group help" + COLOR.WHITE.getValue() + " - 获取指令列表及帮助。",
                    COLOR.GOLD.getValue() + "/group menu" + COLOR.WHITE.getValue() + " - 打开主菜单。",
            });
            return true;
        }

        Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
        Team team = scoreboard.getEntryTeam(player.getName());
        boolean isLeader = team.getName().equals(player.getName());
        switch (getCmd(args[0])) {
            case "prefix":
                if(!isLeader){
                    UTIL.SEND(sender, COLOR.RED.getValue() + "没有使用该指令的权限。");
                    return true;
                }
                if(args.length==1){
                    args = new String[]{"prefix",""};
                }
                UTIL.EXECUTE("team modify "+team.getName()+" prefix \""+args[1]+"\"");
                UTIL.SEND(sender, COLOR.GOLD.getValue() + "操作成功。");
                return true;
            case "suffix":
                if(!isLeader){
                    UTIL.SEND(sender, COLOR.RED.getValue() + "没有使用该指令的权限。");
                    return true;
                }
                if(args.length==1){
                    args = new String[]{"suffix",""};
                }
                UTIL.EXECUTE("team modify "+team.getName()+" suffix \""+args[1]+"\"");
                UTIL.SEND(sender, COLOR.GOLD.getValue() + "操作成功。");
                return true;
            case "join":
                //TODO 加入队伍（申请）
                return true;
            case "leave":
                //TODO 离开队伍（确认）
                return true;
            case "invite":
                //TODO 邀请玩家（申请）
                if(!isLeader){
                    UTIL.SEND(sender, COLOR.RED.getValue() + "没有使用该指令的权限。");
                    return true;
                }
                if(args.length==1){
                    UTIL.SEND(sender, COLOR.RED.getValue() + "请输入目标玩家ID。");
                    return true;
                }
                UTIL.SEND(sender, COLOR.GOLD.getValue() + "操作成功。");
                return true;
            case "kick":
                //TODO 踢出玩家（确认）
                if(!isLeader){
                    UTIL.SEND(sender, COLOR.RED.getValue() + "没有使用该指令的权限。");
                    return true;
                }
                if(args.length==1){
                    UTIL.SEND(sender, COLOR.RED.getValue() + "请输入目标玩家ID。");
                    return true;
                }
                UTIL.SEND(sender, COLOR.GOLD.getValue() + "操作成功。");
                return true;
        }
        return true;
    }

    private String getCmd(String str) {
        List<String[]> list = new ArrayList() {{
            add(new String[]{"?", "help"});
            add(new String[]{"!", "menu"});
            add(new String[]{"pre", "prefix"});
            add(new String[]{"suf", "suffix"});
        }};
        for (String[] strs : list)
            if (UTIL.CHECK(str, strs))
                return strs[strs.length - 1];
        return "";
    }

    public CMDGroup() {
    }

    public CMDGroup(FileConfiguration config) {
        this.conf = new HashMap() {{
        }};
    }

    public CMDGroup(JavaPlugin plugin, FileConfiguration config) {
        this(config);
        //plugin.getCommand("r").setTabCompleter(new CMPGroup());
        Bukkit.getPluginManager().registerEvents(new LSNGroup(), plugin);
    }

}
